import 'font-awesome/css/font-awesome.min.css';
import 'element-plus/dist/index.css';
import 'font-awesome/css/font-awesome.min.css'
// 或
import '@fortawesome/fontawesome-free/css/all.css'

import axios from 'axios';
import ElementPlus from 'element-plus';
import qs from 'qs';
import {createApp} from 'vue';

import App from './App.vue';
import {getCurDate, getLocalStorage, getSessionStorage, removeLocalStorage, removeSessionStorage, setLocalStorage, setSessionStorage} from './common.js';
import router from './router';

// 设置 axios 的基础 URL
axios.defaults.baseURL = 'http://localhost:8080/api/';

// 创建 Vue 应用实例
const app = createApp(App);

// 将 axios 挂载到全局实例上，使用时可以通过 this.$axios 调用
app.config.globalProperties.$axios = axios;
app.config.globalProperties.$qs = qs;
app.config.globalProperties.$getCurDate = getCurDate;
app.config.globalProperties.$setSessionStorage = setSessionStorage;
app.config.globalProperties.$getSessionStorage = getSessionStorage;
app.config.globalProperties.$removeSessionStorage = removeSessionStorage;
app.config.globalProperties.$setLocalStorage = setLocalStorage;
app.config.globalProperties.$getLocalStorage = getLocalStorage;
app.config.globalProperties.$removeLocalStorage = removeLocalStorage;

// 路由守卫
router.beforeEach((to, from, next) => {
  const token = localStorage.getItem('token');
  // 除了登录、注册、首页、商家列表、商家信息之外，都需要判断是否登录
  if (!(to.path === '/' || to.path === '/index' ||
        to.path === '/businessList' || to.path === '/login' ||
        to.path === '/register')) {
    if (token === null) {
      router.push('/login');
    }
  }
  next();
});

// 挂载应用
app.use(router).mount('#app');
app.use(ElementPlus);